package com.bsoft.advance.core.balancer;

import com.bsoft.advance.core.common.ServiceInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 轮询算法
 */
@Slf4j
public class RoundRobinBalance implements LoadBalance {

    private static volatile int index;

    @Override
    public synchronized ServiceInfo doSelect(List<ServiceInfo> services) {
        // 加锁防止多线程情况下，index超出services.size()
        if (index >= services.size()) {
            index = 0;
        }
        return services.get(index++);
    }
}
